﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using Microhard.Plat.CRM.Customers.DataAccess;
using Microhard.Plat.CRM.Customers.Models;

namespace Microhard.Plat.CRM.Customers.DTO
{
    public static class CustomerServerMapBuilderExtension
    {

        #region SaleLead
        public static SaleLeadModel ToModel(this SaleLead entity)
        {
            var model = Mapper.Map<SaleLeadModel>(entity);
            model.Customer = entity.Customer.ToModel();
            model.CustomerID = entity.Customer.CustomerID;
            return model;
        }

        public static SaleLead ToEntity(this SaleLeadModel model)
        {
            return Mapper.Map<SaleLead>(model);
        }
        #endregion

        #region Customer
        public static CustomerModel ToModel(this Customer entity)
        {
            return Mapper.Map<CustomerModel>(entity);
        }

        public static Customer ToEntity(this CustomerModel model)
        {
            return Mapper.Map<Customer>(model);
        }
        #endregion

        #region Product
        public static ProductModel ToModel(this Product entity)
        {
            return Mapper.Map<ProductModel>(entity);
        }

        public static Product ToEntity(this ProductModel model)
        {
            return Mapper.Map<Product>(model);
        }
        #endregion

        #region Order
        public static OrderModel ToModel(this Order entity)
        {
            var model = Mapper.Map<OrderModel>(entity);
            if (entity.CustomerOrders.Count > 0)
            {
                model.Customer = entity.CustomerOrders.First().Customer.ToModel();
            }
            return model;
        }

        public static Order ToEntity(this OrderModel model)
        {
            return Mapper.Map<Order>(model);
        }
        #endregion

        #region OrderDetail
        public static OrderDetailModelInServer ToModel(this OrderDetail entity)
        {
            return Mapper.Map<OrderDetailModelInServer>(entity);
        }

        public static OrderDetail ToEntity(this OrderDetailModelInServer model)
        {
            return Mapper.Map<OrderDetail>(model);
        }
        #endregion

        #region CustomerCoin
        public static CustomerCoinModel ToModel(this CustomerCoin entity)
        {
            var model = Mapper.Map<CustomerCoinModel>(entity);
            if (entity.CustomerCoinOrders.Count > 0)
            {
                model.HistoryOrders = entity.CustomerCoinOrders.Select(p => p.ToModel()).ToList();
            }
            return model;
        }

        public static CustomerCoin ToEntity(this CustomerCoinModel model)
        {
            return Mapper.Map<CustomerCoin>(model);
        }
        #endregion

        #region CustomerCoinOrder
        public static CoinCusumeOrderModel ToModel(this CustomerCoinOrder entity)
        {
            var model = Mapper.Map<CoinCusumeOrderModel>(entity);
            model.Order = entity.CustomerOrder.Order.ToModel();
            return model;

        }

        public static CustomerCoinOrder ToEntity(this CoinCusumeOrderModel model)
        {
            return Mapper.Map<CustomerCoinOrder>(model);
        }
        #endregion
    }
}
